package cn.jg.framework.common.exception;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author fanfever
 * @date 2016/7/11
 */
@Getter
@Setter
@Slf4j
@EqualsAndHashCode(callSuper = false)
@JsonIgnoreProperties({"logger"})
public class ServerException extends BaseException {

	@JsonProperty("message")
	private final String message;
	@JsonProperty("cause")
	private final String causes;
	@JsonProperty("stackTrace")
	private final List<StackTraceElement> stackTraceList;

	public ServerException(String bizCode, Exception ex) {
		super(bizCode);
		this.message = ex.getMessage();
		this.causes = ex.getClass().getName();
		this.stackTraceList = Arrays.stream(ex.getStackTrace())
				.filter(i -> i.getClassName().startsWith("com.github.fanfever.fever.") || i.getClassName().startsWith("cn.udesk."))
				.collect(Collectors.toList());
		log.error(causes, ex);
	}

	public ServerException(String bizCode, String message) {
		super(bizCode);
		causes = null;
		stackTraceList = null;
		this.message = message;
		log.error(message);
	}

}
